package com.example.ying.task;

import com.example.ying.model.domain.CommentLiked;
import com.example.ying.service.CommentLikedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CommentTask {
    @Autowired
    private CommentLikedService commentLikedService;
    @Transactional
    //@Scheduled()
    public void transLikedFromRedis2DB(){
        List<CommentLiked> list = commentLikedService.getLikedDataFromRedis();
        for (CommentLiked like : list) {
            CommentLiked one = commentLikedService.existByLikedCommentIdAndLikedUserId(like.getLikedCommentId(), like.getLikedUserId());
            if (one == null){
                //没有记录，直接存入
                commentLikedService.saveOrUpdate(like);
            }else{
                //有记录，需要更新
                like.setId(one.getId());
                commentLikedService.saveOrUpdate(like);
            }
        }
        System.out.println("数据库同步成功");
    }
}
